// pages/subscribe-list/subscribe-list.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    subscribes: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAndShowData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getAndShowData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 去订阅提醒详细页

  toSubscribeDetail: function (e) {
    // console.log(e.currentTarget.dataset.index)
    app.subscribePage = {
      type: 'detail',
      subscribe: this.data.subscribes[e.currentTarget.dataset.index]
    }
    wx.navigateTo({
      url: '/pages/subscribe-detail/subscribe-detail',
    })
    // console.log(app.subsribePage)
  },

  // 去添加订阅页
  toAddSubscribe: function () {
    app.subscribePage = {
      type: 'add'
    }
    wx.navigateTo({
      url: '/pages/subscribe-detail/subscribe-detail',
    })
  },

  // 获取并显示数据
  getAndShowData: function () {
    if (app.token == null || app.token == "") {
      wx.login({
        complete: (res) => {
          var code = res.code
          wx.request({
            url: that.userLoginApi,
            data: {
              code: code
            },
            success(res) {
              if (res.data.code == 0) {
                that.token = res.data.result.token
                wx.setStorageSync('token', that.token)
                this.getAndShowSubscribes()
              }
            }
          })
        },
      })
    } else {
      this.getAndShowSubscribes()
    }
  },

  // 获取并显示订阅列表
  getAndShowSubscribes(){
    var that = this
    wx.request({
      url: app.subscribeListApi,
      data:{
        token:app.token
      },
      success(res){
        if(res.data.code==0){
          console.log(res)
          that.setData({
            subscribes:res.data.result.subscribes
          })
          wx.stopPullDownRefresh({
            complete: (res) => {},
          })
        }
      }
    })
  }

})